package cn.job.j2

object Main {

  def main(args: Array[String]): Unit = {
    val computer = new Computer()
    val user = new User()
    println("-" * 20 + "欢迎进入游戏世界" + ("-" * 20))
    println("*" * 50)
    println("*" * 22 + "猜拳开始" + ("*" * 22))
    println("*" * 50)
    val game = new Game()

    println(s"请选择对战角色:(${game.getRole})")
    val role = scala.io.StdIn.readInt()
    computer.setName(game.role.getOrElse(role, ""))
    println(s"你选择了与 ${computer.getName} 对战")
    val flag: Boolean = true
    while (flag) {
      println(s"请出拳:(${game.getFist})")
      var fist = scala.io.StdIn.readInt()
      var userFist = game.fist.getOrElse(fist, "")
      if (userFist == "") {
        println("出拳非法,默认出布")
        fist = 3
        userFist = game.fist.getOrElse(fist, "")
      }
      println(s"你出拳:  ${userFist}")

      val robotFist = computer.showFist()
      println(s"${computer.getName}出拳:${game.fist.getOrElse(robotFist, "")} ")
      val diff = game.diff(fist, robotFist)
      val diff2 = game.diff(robotFist, fist)
      println(diff, diff2, fist, robotFist)
      if (diff == diff2) {
        println(s"结果: 和局！ 下次继续努力")
        game.computer("和") = game.computer.getOrElse("和", 0) + 1
        game.guest("和") += 1
        computer.score += 1
        user.score += 1
      } else {
        if (diff2 > 0) {
          computer.score += diff2
          game.computer("胜") += 1
          game.guest("负") += 1
          println(s"结果: ${computer.getName} 赢啦!")
        }
        if (diff > 0) {
          user.score += diff
          game.guest("胜") += 1
          game.computer("负") += 1
          println(s"结果: 恭喜你赢啦!")
        }
      }
      game.vsCount += 1
      println("是否开启下一局? (y/n)")
      val next = scala.io.StdIn.readLine()
      if (next == "n") {
        println("退出游戏")
        println("-" * 50)
        println(s"${computer.getName} VS 游客")
        println(s"对战次数${game.vsCount}")
        println(f"姓名\t得分\t胜局\t和局\t负局")
        println(f"${computer.getName}\t${computer.getScore}\t${game.computer.getOrElse("胜", 0)}\t${game.computer.getOrElse("和", 0)}\t${game.computer.getOrElse("负", 0)}")
        println(f"${user.getName}\t${user.getScore}\t${game.guest.getOrElse("胜", 0)}\t${game.guest.getOrElse("和", 0)}\t${game.guest.getOrElse("负", 0)}")
        return
      }

    }


  }
}
